<?php
require_once 'autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Http\RequestCore;
use OSS\Http\ResponseCore;

class Oss 
{
		
	function ossupload($config,$files,$is_water=2){
		
		$accessKeyId = $config['access_key_id'];
		$accessKeySecret = $config['access_key_secret'];
		// Endpoint以杭州为例，其它Region请按实际情况填写。
		$endpoint = 'http://'.$config['endpoint'];
		// 存储空间名称
		$bucket= $config['bucket'];
		// 文件名称
		if($files['name']=='blob'||$is_water=='1'){
			$upload_name= 'dome.jpg';
		}else{
			$upload_name = $files['name']; 
		} 
		$str="0123456789abcdefghijklmnopqrstuvwxyz";
	    $key = "";	
	    for($i=0;$i<4;$i++){
	        $key .= $str{mt_rand(0,32)};
	    }		
		$object = 'sxkj_offline/'.date('Y-m-d').'/'.uniqid().$key.strrchr($upload_name, '.');
		if($is_water=='1'){
			$filePath = $files;
		}else{
			// <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
			$filePath = $files["tmp_name"];
		}
		
		
		try{
		    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
		
		    $res = $ossClient->uploadFile($bucket, $object, $filePath);
		    
		    if($res['info']['http_code']==200){
		    	//return $res;
		    	return $object;
		    }else{
		    	return 'error';	
		    }   
		} catch(OssException $e) {
		    //printf(__FUNCTION__ . ": FAILED\n");
		    //printf($e->getMessage() . "\n");
		    return 'error';
		}		
	}
	
	function ossread($config,$files){
		
		$accessKeyId = $config['access_key_id'];
		$accessKeySecret = $config['access_key_secret'];
		// Endpoint以杭州为例，其它Region请按实际情况填写。
		$endpoint = 'http://'.$config['endpoint'];
		// 存储空间名称
		$bucket= $config['bucket'];	
		$object = $files;
		$securityToken = null;
		
		$timeout = 3600;
		try {
		    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken);
		
		    // 生成GetObject的签名URL。
		    $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);
		   
		    return $signedUrl; 
		} catch (OssException $e) {
		    printf(__FUNCTION__ . ": FAILED\n");
		    printf($e->getMessage() . "\n");
		    return;
		}
		
	}
	
	
}

?>